package org.auth.center.service;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.auth.center.entity.Account;
import org.auth.center.mapper.AccountMapper;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * @Description
 * @E-mail cwm3412638@163.com
 * @Author cwm
 * @Date 2024/8/13
 */
@Service
public class UserDetailsServiceImpl extends ServiceImpl<AccountMapper, Account> implements UserDetailsService {

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        Account account = getBaseMapper().selectOne(Wrappers.<Account>lambdaQuery().eq(Account::getUsername, username));
        if (Objects.isNull(account)) {
            throw new UsernameNotFoundException("用户不存在");
        }
        return new User(account.getUsername(), account.getPassword(), account.getEnabled(), account.getAccountNonExpired(), account.getCredentialsNonExpired(), account.getAccountNonLocked(), AuthorityUtils.NO_AUTHORITIES);
    }
}
